/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package banvetauserver;

import dal.user;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import pojo.Orders;
import pojo.Users;

/**
 *
 * @author XuanHung
 */
public class BanVeTauServer {

    public static ServerSocket srvSocket = null;
    public static ObjectInputStream cis = null;
    public static ObjectOutputStream cos = null;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try {
            ServerSocket srvSocket = new ServerSocket(10000);
            System.out.println("Waiting...");
            Socket client = srvSocket.accept();
            System.out.println("Have client...");

            cos = new ObjectOutputStream(client.getOutputStream());
            cis = new ObjectInputStream(client.getInputStream());
            System.out.println("Connected to stream...");
            int funcNunber = cis.read();

            if (funcNunber == 1) {
                System.out.println("Login Function");
                byte[] infoBytes = new byte[cis.read()];
                cis.read(infoBytes);
                String[] s = (String[]) Serializer.deserialize(infoBytes);
                System.out.println(s[0] + "," + s[1]);
                pojo.Users us = new Users();
                us.setEmail(s[0]);
                us.setPassword(user.MD5(s[1]));
                boolean result = dal.user.login(us);
                cos.writeBoolean(result);
            } else if (funcNunber == 2) {
                System.out.println("Register Function");
                byte[] infoBytes = new byte[cis.read()];
                cis.read(infoBytes);
                String[] s = (String[]) Serializer.deserialize(infoBytes);
                System.out.println(s[0] + "," + s[1]);
                pojo.Users us = new Users();
                us.setEmail(s[0]);
                us.setPassword(user.MD5(s[1]));
                boolean result = dal.user.register(us);
                cos.writeBoolean(result);
            } else if (funcNunber == 2) {
                System.out.println("Buy Ticket Function");
                byte[] infoBytes = new byte[cis.read()];
                cis.read(infoBytes);
                String[] s = (String[]) Serializer.deserialize(infoBytes);
                System.out.println(s[0] + "," + s[1]);
                pojo.Orders ord = new Orders();
                ord.setTrain_id(Integer.parseInt(s[0]));
                ord.setAmount(Integer.parseInt(s[1]));
                ord.setTotal(Double.parseDouble(s[1]));
                boolean result = dal.order.add_order(ord);
            } else {
                System.out.println("Logout Function");
                byte[] infoBytes = new byte[cis.read()];
                cis.read(infoBytes);
                String[] s = (String[]) Serializer.deserialize(infoBytes);
                System.out.println(s[0] + "," + s[1]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        //process
//        dal.user us = new user();
//        pojo.Users user = new pojo.Users();
//        us.login(user);
    }

}
